package tsing.good_simple.timerImpl.scheduler;

import tsing.good_simple.timerImpl.job.AlarmJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Date 2024/8/2 11:28
 * @Author Tsing
 */
@Configuration
public class QuartzConfig {




    /**
     * 创建JobDetail
     * @return
     */
    @Bean
    public JobDetail alarmJobDetail() {
        return JobBuilder.newJob(AlarmJob.class)
                .withIdentity("alarmJob")
                .storeDurably()
                .build();
    }


    /**
     * 创建触发器 ,调度器，并执行JOb:开启定时任务
     * @return
     */
    @Bean
    public Trigger alarmJobTrigger() {
        ScheduleBuilder scheduleBuilder = SimpleScheduleBuilder
                .simpleSchedule()
                .withIntervalInSeconds(1) // 每秒检查
                .withRepeatCount(1);// 只执行1次
//                .repeatForever();

        // 使用Cron表达式设定闹钟时间
        String cronExpression = "00 26 14 * * ?"; // 每天08:00触发
        return TriggerBuilder.newTrigger()
                .forJob(alarmJobDetail())
                .withIdentity("alarmTrigger")
                .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression))
//                .withSchedule(scheduleBuilder)
                .build();
    }


}
